package com.jzd1997;

/**
 * @author jiangzd
 * @date 2019-08-13 10:41
 * @email 39627020@qq.com
 */
public class BinarySearch {
    public static int halfSearch(int[] arr,int val){
        int begin,end,mid;
        begin = 0;
        end = arr.length - 1;
        while(begin<=end){
            mid = (begin + end)/2;
            if(arr[mid]>val){
                end = mid - 1;
            }else if(arr[mid]==val){
                return mid;
            }else{
                begin = mid + 1;
            }
        }
        return -1;
    }

    public static void main(String[] args){
        int[] arr = new int[]{1,3,4,5,6,9};
        System.out.println(halfSearch(arr,4));
        System.out.println(halfSearch(arr,1));
        System.out.println(halfSearch(arr,8));
        System.out.println(halfSearch(arr,9));
    }
}